package de.koller.zero.view;

import de.koller.zero.ZeroRoot;
import de.koller.zero.engine.commands.*;
import de.koller.zero.view.builder.RealmComp;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.Window.Type;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

public class ZRealmFrame extends ZFrameNode {

	public static final String ADD_PLAYER = "Spieler";
	public static final String ADD_ROUND = "Runde";
	
	private final RealmComp comp = new RealmComp( this );
	
	@Override
	public void generate( ZeroRoot root, Object... references ) {
		super.generate( root, references );

		comp.add.addActionListener( new AddListener() );
		comp.save.addActionListener( new SaveListener() );
		comp.realmVal.addMouseListener( new StatusTableListener() );
		
		frame.add( comp );
		frame.pack();
	}

	@Override
	protected void update( Object... refernces ) {
		super.update( refernces );
	
		
		comp.realmVal.getModel().setValueAt( root.realm.getRound() + "", 0, 1 );
		comp.realmVal.getModel().setValueAt( root.realm.getPlayerNumber() + "", 1, 1 );
		
//		comp.round.setText( root.realm.getRound() + "" );
//		comp.playerNumber.setText( root.realm.getPlayerNumber() + "" );
	}
	
	
	@Override
	protected Type getFrameType() {
		return Window.Type.NORMAL;
	}
	
	@Override
	protected void frameClosed() {
		root.exit();
	}

	@Override
	protected Rectangle getLocation() {
		return new Rectangle( 100, 100, 300, 500 );
	}

	@Override
	protected String getTitle() {
		return ZeroRoot.TITLE;
	}
	
	public void openPlayerList() {
		this.root.gui.call( new AddNode( this, new PlayerListFrame() ) );
	}
	
	private class SaveListener implements ActionListener {

		@Override
		public void actionPerformed( ActionEvent e ) {
			root.engine.call( new SaveGame() );
		}
		
	}
	
	private class StatusTableListener implements MouseListener {

		@Override public void mouseClicked( MouseEvent e ) {
			if( comp.realmVal.rowAtPoint( e.getPoint() ) == 1 )
				openPlayerList();
		}
		
		@Override public void mouseEntered( MouseEvent e ) {}
		@Override public void mouseExited( MouseEvent e ) {}
		@Override public void mousePressed( MouseEvent e ) {}
		@Override public void mouseReleased( MouseEvent e ) {}
		 
	}
	
	private class AddListener implements ActionListener {

		@Override
		public void actionPerformed( ActionEvent e ) {
			int r = 0;
			try {
				r = Integer.parseInt( ZRealmFrame.this.comp.addNumber.getText() );
			} catch( Exception ex ) {}
			
			String v = (String)ZRealmFrame.this.comp.addChoose.getSelectedItem();
			if( v.equals( ADD_ROUND ) ) ZRealmFrame.this.root.engine.call( new UpdateRound( r ) );
			if( v.equals( ADD_PLAYER ) ) ZRealmFrame.this.root.engine.call( new AddPlayer( r ) );
		}
		
	}
	
	public static ComboBoxModel<String> getAddModel() {
		return new AddCBModel();
	}
	
	private static class AddCBModel implements ComboBoxModel<String> {
		private String choosed = ZRealmFrame.ADD_ROUND;
		
		@Override public Object getSelectedItem() {
			return choosed;
		}

		@Override public void setSelectedItem( Object anItem ) {
			choosed = (String)anItem;
		}

		@Override
		public void addListDataListener( ListDataListener l ) {
			
		}

		@Override
		public String getElementAt( int index ) {
			if( index == 0 ) return ADD_ROUND;
			if( index == 1 ) return ADD_PLAYER;
			throw new IllegalArgumentException();
		}

		@Override
		public int getSize() {
			return 2;
		}

		@Override
		public void removeListDataListener( ListDataListener l ) {
			
		}
		
	}
	
}
